<?php

namespace project\common;

use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;

class MonologHandlerDb extends AbstractProcessingHandler
{

    /**
     * @param int $level
     * @param bool $bubble 多个handler将不会处理
     */
    public function __construct(int $level = Logger::ERROR, bool $bubble = true)
    {
        parent::__construct($level, $bubble);
    }

    public function isHandling(array $record): bool
    {
        return $record['level'] >= $this->level;
    }

    protected function write(array $record): void
    {
        // TODO: 实现读取项目中的实现
        try {
            $msg = empty($record['context']) ? '' : json_encode($record['context'], JSON_UNESCAPED_UNICODE);
            \model\MMemoryLogs::insert(['title' => $record['message'], 'msg' => $msg]);
        } catch (\Throwable $e) {

        }
    }
}